import dayjs from "dayjs";

export default {
	getCurrentDateTime() {
		let current = new Date();
		let year = current.getFullYear();
		let month = current.getMonth() + 1;
		if (month < 10) {
			month = '0' + month
		}
		let day = current.getDate();
		if (day < 10) {
			day = '0' + day
		}
		let hour = current.getHours();
		if (hour < 10) {
			hour = '0' + hour
		}
		let mins = current.getMinutes();
		if (mins < 10) {
			mins = '0' + mins
		}
		let second = current.getSeconds();
		if (second < 10) {
			second = '0' + second
		}
		return year + '-' + month + '-' + day + ' ' + hour + ':' + mins + ':' + second
	},
	getCurrentDate() {
		let current = new Date();
		let year = current.getFullYear();
		let month = current.getMonth() + 1;
		if (month < 10) {
			month = '0' + month
		}
		let day = current.getDate();
		if (day < 10) {
			day = '0' + day
		}
		return year + '-' + month + '-' + day
	},
	//获取指定时间的日期
	getAssignDate(days, time) {
		let current = new Date(days);
		if (time !== "" || time !== null) {
			current.setDate(current.getDate() + time)
		}
		let year = current.getFullYear();
		let month = current.getMonth() + 1;
		if (month < 10) {
			month = '0' + month
		}
		let day = current.getDate();
		if (day < 10) {
			day = '0' + day
		}
		return year + '-' + month + '-' + day
	},
	//获取时间区间的日期数组
	getDateArr(start, end) {
		let dateArr = [];
		let startArr = start.split("-");
		let endArr = end.split("-");
		let db = new Date();
		db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2]);
		let de = new Date();
		de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2]);
		let unixDb = db.getTime();
		let unixDe = de.getTime();
		let stamp;
		const oneDay = 24 * 60 * 60 * 1000;
		for (stamp = unixDb; stamp <= unixDe;) {
			dateArr.push(this.formatDate(new Date(parseInt(stamp))));
			stamp = stamp + oneDay;
		}
		// echartsData.xdata = dateArr;
		return dateArr;
	},
	formatDate(time) {
		let ymd = "";
		let mouth =
			time.getMonth() + 1 >= 10 ?
			time.getMonth() + 1 :
			"0" + (time.getMonth() + 1);
		let day = time.getDate() >= 10 ? time.getDate() : "0" + time.getDate();
		ymd += time.getFullYear() + "-"; // 获取年份。
		ymd += mouth + "-"; // 获取月份。
		ymd += day; // 获取日。
		return ymd; // 返回日期。
	},
	//分钟转换时分
	formatMin(cellValue) {
		let value = "00:00";
		if (cellValue === 0) return value;
		let valueM = cellValue * 60;
		let minM = Math.floor(valueM % 3600);
		let hour = Math.floor(valueM / 3600);
		let min = Math.floor(minM / 60);
		let sec = Math.floor(valueM % 60);
		if (hour === 0) {
			hour = "00"
		} else if (hour > 0 && hour < 10) {
			hour = "0" + hour
		}
		if (min === 0) {
			min = "00"
		} else if (min > 0 && min < 10) {
			min = "0" + min
		}
		if (sec === 0) {
			sec = "00"
		} else if (sec > 0 && sec < 10) {
			sec = "0" + sec
		}
		return value = hour + ":" + min
	},
	dealMenu(originalMenus, realMenus) {
		originalMenus.forEach(item => {
			if (item.permission) {
				let menu = {};
				menu.name = item.name;
				menu.path = item.path;
				menu.permission = item.permission;
				menu.children = [];
				if (item.children && item.children.length > 0) {
					this.dealMenu(item.children, menu.children)
				}
				realMenus.push(menu)
			}
		})
	},
	//会议预约会议时间处理
	dealMeetingTime(time, type) {
		if (!time) return "";
		let current = new Date(time);
		if (type === 0) {
			//开始时间
			let min = current.getMinutes();
			if (min > 0 && min < 30) {
				current.setMinutes(0)
			}
			if (min > 30 && min < 60) {
				current.setMinutes(30)
			}
		}
		if (type === 1) {
			//结束时间
			let min = current.getMinutes();
			if (min > 0 && min < 30) {
				current.setMinutes(30)
			}
			if (min > 30 && min < 60) {
				current.setDate(current.getDate() + 1);
				current.setMinutes(0)
			}
		}
		let year = current.getFullYear();
		let month = current.getMonth() + 1;
		if (month < 10) {
			month = '0' + month
		}
		let day = current.getDate();
		if (day < 10) {
			day = '0' + day
		}
		let hour = current.getHours();
		if (hour < 10) {
			hour = '0' + hour
		}
		let mins = current.getMinutes();
		if (mins < 10) {
			mins = '0' + mins
		}
		let second = current.getSeconds();
		if (second < 10) {
			second = '0' + second
		}
		return year + '-' + month + '-' + day + ' ' + hour + ':' + mins + ':' + second;
	},
	/**
	 * 获取屏幕比率
	 * @returns {string}
	 */
	getWindowRatio() {
		return (window.innerWidth / 1920).toFixed(1);
	},
	/* 获取指定日期所在月的第一天和最后一天 */
	getMonthStartAndEnd(dateString) {
		const date = dateString ? new Date(dateString) : new Date();
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		// const firstDay = new Date(year, month - 1, 1);
		const lastDay = new Date(year, month, 0);
		const formattedFirstDay = `${year}-${month.toString().padStart(2, '0')}-01`;
		const formattedLastDay = `${year}-${month.toString().padStart(2, '0')}-${lastDay.getDate()}`;
		return {
			firstDay: formattedFirstDay,
			lastDay: formattedLastDay
		};
	},
	/*获取下个月的第一天*/
	nextMonthFirstDay() {
		let time = new Date();
		let year = time.getFullYear();
		let month = time.getMonth() + 2;
		if (month > 12) {
			month = month - 12;
			year = year + 1;
		}
		let day = 1;
		return year + '-' + month + '-' + day;
	},
	/*获取下个月的最后一天*/
	nextMonthLastDay() {
		let time = new Date();
		let year = time.getFullYear();
		let month = time.getMonth() + 2;
		if (month > 12) {
			month = month - 12;
			year = year + 1;
		}
		let day = this.nextMonthDay(year, month);
		return year + '-' + month + '-' + day;
	},
	nextMonthDay(year, month) { //判断每月多少天
		let day31 = [1, 3, 5, 7, 8, 10, 12];
		let day30 = [4, 6, 9, 11];
		if (day31.indexOf(month) > -1) {
			return 31;
		} else if (day30.indexOf(month) > -1) {
			return 30;
		} else {
			if (this.isLeapYear(year)) {
				return 29;
			} else {
				return 28;
			}
		}
	},
	isLeapYear(year) { //判断是否为闰年
		return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
	},
	/* 获取上个月第一天 */
	getLastMonthDay() {
		let newData = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1);
		return dayjs(newData).format('YYYY-MM-DD');
	},
	/* 获取上个月最后一天 */
	getLastMonthLastDay() {
		let date = new Date();
		let day = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
		let enddate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, day);
		return dayjs(enddate).format('YYYY-MM-DD');
	},
	//获取上个月的第一天
	getLastMonthFirstDay() {
		let today = new Date(); // 当前日期
		let firstDayOfThisMonth = new Date(today.getFullYear(), today.getMonth(), 1); // 本月第一天
		let lastMonth = new Date(firstDayOfThisMonth.getFullYear(), firstDayOfThisMonth.getMonth() - 1, 1); // 上个月的第一天

		// 找到上个月的最后一天
		let lastDayOfLastMonth = new Date(lastMonth.getFullYear(), lastMonth.getMonth() + 1, 0);

		// 将上个月的第一天设置为最后一天的最小日期值
		lastMonth.setDate(1);
		return dayjs(lastMonth).format('YYYY-MM-DD');
	},
	// 获取本周第一天
	getFirstDayOfThisWeek() {
		const currentDate = new Date();
		const weekday = currentDate.getDay();
		const firstDay = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() - weekday);
		return firstDay;
	},
	// 获取本周最后一天
	getLastDayOfThisWeek() {
		const firstDay = this.getFirstDayOfThisWeek();
		const lastDay = new Date(firstDay.getFullYear(), firstDay.getMonth(), firstDay.getDate() + 6);
		return lastDay;
	},
	// 获取某年的第一天 //2024
	getFirstDayOfYear(year) {
		return dayjs(new Date(year, 0, 1)).format('YYYY-MM-DD');
	},
	// 获取某年的最后一天 //2024
	getLastDayOfYear(year) {
		return dayjs(new Date(year, 11, 31)).format('YYYY-MM-DD');
	},
	//获取当前时间的前后几天、前后几周、前后几个月、前后几年的时间
	getDateFrontAndBack(type = null, number = 0) {
		var nowdate = new Date();
		switch (type) {
			case "day": //取number天前、后的时间
				nowdate.setTime(nowdate.getTime() + (24 * 3600 * 1000) * number);
				var y = nowdate.getFullYear();
				var m = nowdate.getMonth() + 1;
				var d = nowdate.getDate();
				var retrundate = y + '/' + m + '/' + d;
				break;
			case "week": //取number周前、后的时间
				var weekdate = new Date(nowdate + (7 * 24 * 3600 * 1000) * number);
				var y = weekdate.getFullYear();
				var m = weekdate.getMonth() + 1;
				var d = weekdate.getDate();
				var retrundate = y + '/' + m + '/' + d;
				break;
			case "month": //取number月前、后的时间
				nowdate.setMonth(nowdate.getMonth() + number);
				var y = nowdate.getFullYear();
				var m = nowdate.getMonth() + 1;
				var d = nowdate.getDate();
				var retrundate = y + '/' + m + '/' + d;
				break;
			case "year": //取number年前、后的时间
				nowdate.setFullYear(nowdate.getFullYear() + number);
				var y = nowdate.getFullYear();
				var m = nowdate.getMonth() + 1;
				var d = nowdate.getDate();
				var retrundate = y + '/' + m + '/' + d;
				break;
			default: //取当前时间
				var y = nowdate.getFullYear();
				var m = nowdate.getMonth() + 1;
				var d = nowdate.getDate();
				var retrundate = y + '/' + m + '/' + d;
		}
		return retrundate;
	},
	//计算两个日期时间之间的时长
	durationBetweenDates(date1, date2) {
		if (!date1 || !date2) return "";
		const oneDay = 24 * 60 * 60 * 1000; // 每天的毫秒数
		const diff = Math.abs(date2 - date1); // 计算两个日期时间的差值

		const milliseconds = diff;
		const seconds = diff / 1000;
		const minutes = diff / (1000 * 60);
		const hours = diff / (1000 * 60 * 60);
		const days = diff / (1000 * 60 * 60 * 24);
		const months = days / 30;
		const years = days / 365;

		return {
			milliseconds, //毫秒
			seconds, //秒
			minutes, //分钟
			hours, //小时
			days, //天
			months, //月
			years, //年
		};
		// 示例使用：
		// const date1 = new Date('2023-01-01T12:00:00');
		// const date2 = new Date('2023-04-01T12:00:00');
		// const duration = durationBetweenDates(date1, date2);
	},
	//生成随机rgb颜色
	getRandomRGBA(a = 1) {
		//a 透明度
		const r = Math.round(Math.random() * 255); // 红色值
		const g = Math.round(Math.random() * 255); // 绿色值
		const b = Math.round(Math.random() * 255); // 蓝色值
		// const a = Math.random().toFixed(2); // 透明度值
		return `rgba(${r}, ${g}, ${b}, ${a})`;
	},
	getFirstMondayOfMonth() {
		// 获取当前日期
		var currentDate = new Date();

		// 设置日期为当前月份的第一天
		currentDate.setDate(1);

		// 获取当前月份第一天的星期几（0表示星期日，1表示星期一，以此类推）
		var firstDayOfWeek = currentDate.getDay();

		// 计算第一个星期一距离当前月份第一天的天数
		// 星期日算作一周的最后一天，所以如果是星期日（0），则需要加7天
		var daysToFirstMonday = (firstDayOfWeek <= 1 ? 8 - firstDayOfWeek : 1 - firstDayOfWeek) + 7;

		// 根据天数计算出第一个星期一的日期
		currentDate.setDate(currentDate.getDate() + daysToFirstMonday);

		// 输出第一个星期一的日期
		return currentDate;
		// // 调用函数并输出结果
		// console.log(getFirstMondayOfMonth());
	},
	isWeekdayBetween(startDate, endDate, targetWeekday) {
		// 将输入转换为Date对象
		const start = new Date(startDate);
		const end = new Date(endDate);

		// 确保开始日期小于结束日期
		if (start > end) {
			[start, end] = [end, start];
		}

		// 遍历日期范围，检查每一天的周几
		do {
			if (start.getDay() === targetWeekday) {
				// 找到了匹配的周几
				return true;
			}
			// 将日期增加一天
			start.setDate(start.getDate() + 1); //0代表周日，1代表周一，以此类推
		} while (start <= end);

		// 没有找到匹配的周几
		return false;
		// 使用示例 //console.log(isWeekdayBetween('2023-03-01', '2023-03-15', 1)); // 检查这个范围内是否有周一
	},
	//校验两个日期之间有多少天并返回每一天的数字
	eacReturnhDay(startDate, endDate) {
		let currentDate = new Date(startDate);
		const end = new Date(endDate);
		const days = [];

		while (currentDate <= end) {
			days.push(new Date(currentDate));
			currentDate.setDate(currentDate.getDate() + 1);
		}

		return days;
		// 示例使用
		// const startDate = '2023-01-01';
		// const endDate = '2023-01-05';
		// const daysArray = eachDay(startDate, endDate);
	},


}